首页 > 学习笔记 > [FaceBook] upload image – fan page(上传用户相片到客户专页相册的方式)
2011
05-16

[FaceBook] upload image – fan page(上传用户相片到客户专页相册的方式)

1.使用fanpage admin和app admin登陆

在config.php修改取得用户权限代码。 

//define(“APP_PERM”,”publish_stream,user_photos,email,user_birthday,user_interests,user_likes”);//正常取得用户权限
define(“APP_PERM”,”publish_stream,user_photos,email,user_birthday,user_interests,user_likes,offline_access”);//添加offline_access

2.弹出allow得到offline_access。

3.通过facebook api得到access_token。

   $access_token = $facebook->getAccessToken();

   例如:170400923015461|59f852ffb6a063ec8c44b880.1-100001720570055|fduZ2UPXVyWdagfQ-nsNtx1J3eg

4.恢复。

    //define(“APP_PERM”,”publish_stream,user_photos,email,user_birthday,user_interests,user_likes”);//正常取得用户权限

5.app程序使用以上得到的access_token,上传图片到fanpage相册。

详细代码如下:

// to get authority of  fanpage
pageauth.php
//必须使用fanpage admin访问一次取得fanpage权限

<?php
include_once ‘config.php’; //配置文件
include_once ‘init.php’;//facebook api

$facebook = new Facebook(array(
  ‘appId’ => APP_ID,
  ‘secret’=> APP_SECRET,
  ‘cookie’=> true
  ));
$next =
‘https://www.facebook.com/connect/prompt_permissions.php?api_key=’. APP_KEY.
‘&fbconnect=true&v=1.0&display=popup&extern=1&next=’. FB_PATH.
‘&ext_perm=publish_stream&enable_profile_selector=1&profile_selector_ids=’. FANPAGE_ID
;

echo ‘<script language=”javascript”>top.location = ‘.$next.’;</script>’;
?>

<?php
function getAlbumFP($albumname,$albumdescription,$owner,$access_token=”){
 global $facebook; 
 $facebook->setFileUploadSupport(1);  
 $albumlist = $facebook->api(array(
  ‘method’ => ‘fql.query’,
  ‘query’ => ‘SELECT object_id, name, aid, size FROM album WHERE owner = ‘.$owner
 )); 
 if(sizeof($albumlist)>0){
  foreach($albumlist as $album) {
     if($album[“size”] != 200 && $album[“name”] == $albumname) {
    return substr(strrchr($album[“aid”], ‘_’),1);
    break;
     }
  }
 }
 //no album match, create new album 
 $param = array();
 $param[“method”] = “photos.createAlbum”;
 $param[“name”] = $albumname;
 $param[“description”] = $albumdescription;
 $param[“visible”] = ‘everyone’;
 $param[“uid”] = $owner;
 if($access_token != ”){
  $param[“access_token”] = $access_token;
 }
 $result = $facebook->api($param);
 return substr(strrchr($result[“aid”], ‘_’),1);
}

function uploadPhotoSimpleFP($filepath, $caption, $aid, $targetid, $access_token=”){
 global $facebook;
 try {
  $param = array();
  $facebook->setFileUploadSupport(1); 
  $param[“method”] =”photos.upload”;
  $param[“aid”] = $aid;
  $param[“caption”] = $caption;
  $param[“uid”] = $targetid;
  $param[“_file”] = ‘@’ . realpath($filepath);
  if($access_token != ”){
   $param[“access_token”] = $access_token;
  }
  $a = $facebook->api($param);
  return $a;
 }catch(Exception $e){
  print_r($e);
 }
}

$page_access_token = “185854648131926|686b6106eecb450c53d81126.1-100002256837230|X1pInBvhNX4e16F1w5JJ2GJwk0o”;
$albumid = getAlbumFP(APP_NAME, APP_DESCTIPTION, FANPAGE_ID, $access_token); //取相册id, 没有将自动创建
$photoinfo = uploadPhotoSimpleFP(‘需要上传的图片路径’, ‘描述’, $albumid, FANPAGE_ID, $access_token);
?>

最后编辑:
作者:admin
这个作者貌似有点懒,什么都没有留下。

留下一个回复